<?php
class ModelCatalogCategory extends Model {
	public function getCategory($category_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE c.category_id = '" . (int) $category_id . "' AND cd.language_id = '" . (int) $this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int) $this->config->get('config_store_id') . "'");
		
		return $query->row;
	}
	public function getCategories($parent_id = 0) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE c.parent_id = '" . (int) $parent_id . "' AND cd.language_id = '" . (int) $this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int) $this->config->get('config_store_id') . "'  AND c.status = '1' ORDER BY c.sort_order, LCASE(cd.name)");
		
		return $query->rows;
	}
	public function getCategoriesByParentId($category_id,$cache=1) {
		if($cache){
			$category_data = $this->cache->get('categorieByParentId.' . $category_id);	
		}
		if(!$category_data){
			$category_query = $this->db->query("SELECT category_id FROM " . DB_PREFIX . "category WHERE parent_id = '" . (int) $category_id . "'");
		
			foreach ($category_query->rows as $category) {
				$category_data[] = $category['category_id'];
				
				$children = $this->getCategoriesByParentId($category['category_id'],0);
				
				if ($children) {
					$category_data = array_merge($children, $category_data);
				}
			}
			if($cache){
				$this->cache->set('categorieByParentId.' . $category_id, $category_data);
			}
		}
		
		
		
		return $category_data;
	}
	public function getCategoryLayoutId($category_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category_to_layout WHERE category_id = '" . (int) $category_id . "' AND store_id = '" . (int) $this->config->get('config_store_id') . "'");
		
		if ($query->num_rows) {
			return $query->row['layout_id'];
		}
		else {
			return $this->config->get('config_layout_category');
		}
	}
	public function getTotalCategoriesByCategoryId($parent_id = 0) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE c.parent_id = '" . (int) $parent_id . "' AND c2s.store_id = '" . (int) $this->config->get('config_store_id') . "' AND c.status = '1'");
		
		return $query->row['total'];
	}
	
	/**
	 * 选择筛选条件
	 * @param unknown_type $category_id
	 */
	public function getCatScreen($category_id, $languge_id) {
		$query = $this->db->query("SELECT a.attribute_id,ad.name,pa.text,a.is_default 
				FROM  " . DB_PREFIX . "attribute_group ap 
				JOIN  " . DB_PREFIX . "attribute a ON ap.attribute_group_id=a.attribute_group_id
				JOIN  " . DB_PREFIX . "product_attribute pa ON a.attribute_id=pa.attribute_id 
				JOIN  " . DB_PREFIX . "attribute_description ad ON a.attribute_id=ad.attribute_id 
				WHERE ap.category_id={$category_id} AND pa.text!='' AND pa.language_id={$this->config->get ( 'config_language_id' )} AND a.is_screen=1 GROUP BY pa.text");
		$arr_show = $arr_hid = array();
		if (is_array($query->rows)) {
			foreach ($query->rows as $k => $v) {
				if ($v['is_default'] == 1) {
					$arr_show[$v['attribute_id']][] = $v;
				}
				else {
					$arr_hid[$v['attribute_id']][] = $v;
				}
			}
		}
		return array( 
			'show' => $arr_show, 
			'hid' => $arr_hid 
		);
	}

	public function getCategorieDetailsByParentId($category_id) {

		$category_data = $this->cache->get('categorieDetail.' . $category_id);
		if(!$category_data){
			$category_query = $this->db->query("SELECT c.category_id,cd.name,c.parent_id  
				FROM " . DB_PREFIX . "category c 
				LEFT JOIN " . DB_PREFIX . "category_description cd ON c.category_id=cd.category_id  
				WHERE parent_id = '" . (int) $category_id . "' AND cd.language_id = '" . (int) $this->config->get('config_language_id') . "'
				ORDER BY c.sort_order, LCASE(cd.name)");
		
			foreach ($category_query->rows as $category) {
				$category_data[$category['category_id']]['id'] = $category['category_id'];
				$category_data[$category['category_id']]['name'] = $category['name'];
				$category_data[$category['category_id']]['pid'] = $category['parent_id'];

				$category_sub_query = $this->db->query("SELECT c.category_id,cd.name,c.parent_id  
					FROM " . DB_PREFIX . "category c 
					LEFT JOIN " . DB_PREFIX . "category_description cd ON c.category_id=cd.category_id  
					WHERE parent_id = '" . (int) $category['category_id'] . "' AND cd.language_id = '" . (int) $this->config->get('config_language_id') . "'
					ORDER BY c.sort_order, LCASE(cd.name)");
				$category_sub_data=array();
				foreach ($category_sub_query->rows as $category_sub) {
					$category_sub_data[$category_sub['category_id']]['id'] = $category_sub['category_id'];
					$category_sub_data[$category_sub['category_id']]['name'] = $category_sub['name'];
					$category_sub_data[$category_sub['category_id']]['pid'] = $category_sub['parent_id'];
				}
				if ($category_sub_data) {
					$category_data[$category['category_id']]['children'] = $category_sub_data;
				}
			}
			$this->cache->set('categorieDetail.' . $category_id, $category_data);
		}

		return $category_data;
	}

	public function findCategoryId($arr=array(),$category_id){
		$true=0;
		foreach ($arr as $key => $value) {
			if($category_id==$value['id']){
				$true==$arr['id'];
				break;
			}	
			if($value['children']){
				$true=$this->findCategoryId($value['children'],$category_id);
				if($true!=0){
					break;
				}
			}
			
		}
		return $true;
	}

	public function getCategoryInfoByPid($parent_id){
		//echo "SELECT cd.category_id,cd.name FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE c.parent_id = '" . (int) $parent_id . "' AND cd.language_id = '" . (int) $this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int) $this->config->get('config_store_id') . "'  AND c.status = '1' ORDER BY c.sort_order, LCASE(cd.name)";
		$query = $this->db->query("SELECT cd.category_id,cd.name FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE c.parent_id = '" . (int) $parent_id . "' AND cd.language_id = '" . (int) $this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int) $this->config->get('config_store_id') . "'  ORDER BY c.sort_order, LCASE(cd.name)");		
		return $query->rows;
	}

}
?>